#ifndef _UFO_RENDER_H_
#define _UFO_RENDER_H_

#if defined(_WIN32)
	//#define DX_RENDER 1
	#define OGL_RENDER 1
#else
	#define OGL_RENDER 1
#endif

#include "struct.h"
#include "matrix.h"

#define UFO_TEXT_CALCRECT 1

typedef enum _UFO_RENDER
{
	UFO_RENDER_2D = 2,
	UFO_RENDER_3D = 3,	
}UFO_RENDER;

typedef enum _UFO_TEXTURE_FILTER
{
	UFO_TEXTURE_FILTER_LINE = 1,
	UFO_TEXTURE_FILTER_POINT = 2,
}UFO_TEXTURE_FILTER;

typedef enum _UFO_TEXTURE_WRAP
{
	UFO_TEXTURE_WRAP_REPEAT = 1,
	UFO_TEXTURE_WRAP_CLAMP = 2,
}UFO_TEXTURE_WRAP;

typedef enum _UFO_ALPHABLEND_FACTOR
{
	ALPHABLEND_ZERO = 1,
	ALPHABLEND_ONE,
	ALPHABLEND_SRC_ALPHA,
	ALPHABLEND_DST_ALPHA,
	ALPHABLEND_SRC_COLOR,
	ALPHABLEND_DST_COLOR,
	ALPHABLEND_ONE_MINUS_SRC_ALPHA,
	ALPHABLEND_ONE_MINUS_DST_ALPHA,
	ALPHABLEND_ONE_MINUS_SRC_COLOR,
	ALPHABLEND_ONE_MINUS_DST_COLOR,
}UFO_ALPHABLEND_FACTOR;

typedef enum _RENDER_CMP_FUNC
{
	RENDER_CMP_ALWAYS = 1,
	RENDER_CMP_NEVER = 2,
	RENDER_CMP_EQUAL = 3,
	//UFO_STENCIL_DESR = 3,
}RENDER_CMP_FUNC;

typedef enum _UFO_STENCIL_OP
{
	UFO_STENCIL_REPLACE = 1,
	UFO_STENCIL_KEEP = 2,
	UFO_STENCIL_INCR = 3,
	UFO_STENCIL_DESR = 4,
}UFO_STENCIL_OP;

typedef enum _UFO_MATRIXMODE
{
	UFO_MODELVIEW = 1,
	UFO_PROJECTION = 2,
}UFO_MATRIXMODE;

typedef struct tagTEXTURE
{
#ifdef OGL_RENDER
	unsigned int id;
#else
	void* id;
#endif
	int width;
	int height;

	int width_POT;
	int height_POT;

	UFO_TEXTURE_FILTER filter;
	UFO_TEXTURE_WRAP wrap;
}tagTEXTURE, *HTEXTURE;

typedef struct tagCANVAS
{
	HTEXTURE texture;

#ifdef OGL_RENDER
	unsigned int framebuffer;
	unsigned int depthbuffer;
#else
	void* depthbuffer;
#endif

}tagCANVAS, *HCANVAS;

void ufoAddFreshRect(const ufoRect * lpRect);
void ufoClearFreshRect();
int ufoGetFreshRect(ufoRect * pRect);

int ufoGetScreenRect(ufoRect * pRect);
int ufoSetScreenRect(ufoRect * pRect);

int ufoSetAlphaBlend(int enabled);
int ufoSetAlphaBlendFactor(UFO_ALPHABLEND_FACTOR src, UFO_ALPHABLEND_FACTOR dst);

int ufoClearStencil(int stencil);
int ufoClearDepth(double depth);
int ufoClearColor(float alpha, float red, float green, float blue);

HCANVAS ufoCreateCanvas(float width, float height);
HCANVAS ufoCanvasGetCurrent();
HCANVAS ufoCanvasGetDefault();
int ufoSetCanvas(HCANVAS canvas);
int ufoRenderCanvas(HCANVAS canvas, const ufoRect *rectDst, const ufoColor *color, float alpha, float dpi);
int ufoBlendCanvas(HCANVAS canvas, HTEXTURE textures, const ufoRect *rectDst, const ufoColor *color, float alpha, float dpi);

HTEXTURE ufoGenTextures(const unsigned char *texData, int width, int height, int pixelSize);
int ufoTextureWidth(HTEXTURE texture);
int ufoTextureHeight(HTEXTURE texture);
int ufoDeleteTexture(int texture);
int ufoRenderTexture(int nTexs, HTEXTURE *texture, int nCount, float *Vertex, float *texCoord, const ufoColor *color, float alpha, float dpi);
int ufoSetTextureFilter(HTEXTURE texture, UFO_TEXTURE_FILTER filter, UFO_TEXTURE_WRAP wrap);

int ufoSetStencilTest(int enabled);
int ufoGetStencilTest();
int ufoSetStencilTestRef(int ref);
int ufoGetStencilTestRef();
int ufoSetStencilFunc(RENDER_CMP_FUNC func);
int ufoSetStencilOp(UFO_STENCIL_OP fail, UFO_STENCIL_OP zfail, UFO_STENCIL_OP zpass);

int ufoSetPixelShader(int id);

int ufoRenderPushMatrix(UFO_MATRIXMODE mode);
int ufoRenderPopMatrix(UFO_MATRIXMODE mode);
int ufoRenderIdentityMatrix(UFO_MATRIXMODE mode);
int ufoRenderLoadMatrix(UFO_MATRIXMODE mode, ufoMatrix4 *matrix);
int ufoRenderMultMatrix(UFO_MATRIXMODE mode, ufoMatrix4 *matrix);
int ufoRenderScale(UFO_MATRIXMODE mode, float x, float y, float z);
int ufoRenderRotate(UFO_MATRIXMODE mode, float fradian, float x, float y, float z);
int ufoRenderTranslate(UFO_MATRIXMODE mode, float x, float y, float z);
int ufoRenderOrthoMatrix(UFO_MATRIXMODE mode, float left, float right, float bottom, float top, float nearVal, float farVal);
int ufoRenderGetMatrix(UFO_MATRIXMODE mode, ufoMatrix4 *matrix);
int ufoRenderLoadProjection(UFO_RENDER projection, float width, float height, float fovy);
int ufoGetMartixCount(UFO_MATRIXMODE mode);
int ufoGetMartixByIndex(UFO_MATRIXMODE mode, int index, ufoMatrix4 *matrix);

int ufoSetDepthTest(int enabled);

int ufoSetCullFace(int enabled);

int ufoSetMultiSample(int enabled);
int ufoGetMutilSample(int enabled);

int ufoPushViewPort();
int ufoPopViewPort();
int ufoSetViewPort(const ufoRect *rect);
int ufoGetViewPort(ufoRect *rect);

int ufoRenderText(const char *text, ufoRect *rect, const char *font, int fontSize, int bold, ufoColor color, int format, unsigned char *output);

int ufoRenderPoint(int nCount, float *Vertex, ufoColor color, float dpi);

int ufoPresent2D(float width, float height);
int ufoPresent3D(float width, float height, float fovy);
UFO_RENDER ufoGetPresent(float *width, float *height);

int ufoRestore(void* wParam, void* lParam);

int ufoBeginRender(void* param);

int ufoEndRender(void* param);

int ufoSupportNPOT();

void ufoTest();

#endif